package com.hanyxx.exception;

/**
 * @author layman
 * @date 2021/3/1
 */
public class Demo03 {
    // 模拟数据库中已存在的账号
    private static String[] names = {"IronMan","spiderMan",};

    public static void main(String[] args) {
        try{
            //checkUsername("Layman");
            checkUsername("spiderMan");
            System.out.println("注册成功");
        }catch(RegisterException e){
            e.printStackTrace();
        }
    }
    /**
     * 验证注册的账号是否存在
     */
    public static boolean checkUsername(String username) throws RegisterException {
        for (String name : names) {
            if(name.equals(username)){
                throw new RegisterException("亲,账号【"+ name + "】已经被注册了！");
            }
        }
        return true;
    }
}
//自定义编译异常
class RegisterException extends Exception {

    //空参构造
    public RegisterException() {
    }

    //有参构造
    public RegisterException(String message) {
        super(message);
    }
}
